<?php
/*
 * 	Yamp52 - Yet Another Magical PHP framework
 *	http://code.google.com/p/yamp52/
 *	
 *	Copyright (C) 2009, Krzysztof Drezewski <krzych@krzych.eu>
 *	
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


class Authentication implements Injectable {


	/**
	 * @Service("Session")
	 * @var Session
	 */
	private $SessionContainer;

	/**
	 * @var User
	 */
	private $user;

	/**
	 * @Service("Configuration")
	 * @var Configuration
	 */
	private $Configuration;
	
	/**
	 * @Service("GlobalVarContainer")
	 * @var SmartyGlobalVarContainer
	 */
	private $Layout;

	public function __set($var, $val) {
		$this->$var = $val;
	}

	/**
	 * Perform user authentication check
	 * @param string $userlogin
	 * @param string $password
	 * @return Message|Message|Message|Message
	 */
	public function checkForUser($userlogin, $password) {
		$this->user = Doctrine::getTable('User')
		->findOneBy('login', $userlogin, Doctrine::HYDRATE_RECORD);

		if(empty($this->user)) {
			return new Message(MESSAGE_ERR, "Unknown user");
		}
		elseif($this->user->active === false) {
			return new Message(MESSAGE_ERR, "Account has been blocked");
		}
		elseif(crypt($password, $this->user->password) != $this->user->password) {
			return new Message(MESSAGE_ERR, "Incorrect login or password");
		}

		return new Message(MESSAGE_OK);
	}


	public function doLogin() {
		$this->isLogged = true;
		
		//clear any cache info that resides in session
		$this->SessionContainer->clearRaw();
		$this->SessionContainer->saveRaw('authenticated_user', $this->user->id);
		header('Location: /'.$this->Configuration->get("www.uri"));
		die;
	}

	public function doLogout() {
		$this->isLogged = false;
		$this->user = array();
		$this->SessionContainer->destroySession();
		header('Location: /'.$this->Configuration->get("www.uri"));
		die;
	}

	/**
	 * @return boolean
	 */
	public function isLogged() {

		$info = $this->getUserInfo();
		if($info) {
			$this->user = $info;
			return true;
		}
		return false;
	}

	
	/**
	 * returns info about logged user
	 * 
	 * @return Doctrine_Record
	 */
	public function getUserInfo() {
		static $info; //anyway - cache internally and in the session

		if(!$info) {
			try {
				$info = $this->SessionContainer->getRawObject('UserInfo');
			} catch (Exception $e) {
				$id = $this->SessionContainer->getRaw('authenticated_user');
				if($id) {
					$info = Doctrine::getTable('User')->findOneBy('id', ($id)?$id:0, Doctrine::HYDRATE_RECORD);
					$this->SessionContainer->saveRaw('UserInfo', $info);
				}
			}
		}
		return $info;
	}

}
